package icu.bughub.app.app.ui.components

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Help
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp


@Composable
fun DailyTaskItem(
    title: String = "",
    secondTitle: String = "",
    desc: String = "",
    percent: Float = 0.0f
) {

    val inlineContentId = "inlineContentId"

    val secondTitleAnnotatedString = buildAnnotatedString {
        withStyle(SpanStyle(color = Color(0xff666666), fontSize = 14.sp)) {
            append(secondTitle)
        }
        appendInlineContent(inlineContentId, "[icon]")
    }

    val inlineContent = mapOf(
        Pair(
            inlineContentId,
            InlineTextContent(
                placeholder = Placeholder(
                    width = 22.sp,
                    height = 14.sp,
                    placeholderVerticalAlign = PlaceholderVerticalAlign.Center
                )
            ) {
                Row {
                    Spacer(modifier = Modifier.width(6.dp))
                    Icon(
                        imageVector = Icons.Default.Help,
                        contentDescription = null,
                        tint = Color(0xffeeeeee),
                        modifier = Modifier
                            .size(14.dp)
                            .clickable {
                                // 点击事件
                            }

                    )
                }
            }
        )
    )


    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 12.dp, start = 8.dp, end = 8.dp),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Column(modifier = Modifier.weight(7.5f)) {
            Text(text = title, color = Color(0xff333333), fontSize = 16.sp)
            Spacer(modifier = Modifier.height(4.dp))
            /* Row(
                 verticalAlignment = Alignment.CenterVertically,
                 modifier = Modifier.padding(vertical = 8.dp)
             ) {
                 Text(
                     text = secondTitleAnnotatedString,
                     inlineContent = inlineContent,
                     color = Color(0xff666666), fontSize = 14.sp
                 )
                 *//*Spacer(Modifier.width(6.dp))
                Icon(
                    imageVector = Icons.Default.Help,
                    contentDescription = null,
                    tint = Color(0xff999999),
                    modifier = Modifier.size(16.dp)
                )*//*
            }*/

            Text(
                text = secondTitleAnnotatedString,
                inlineContent = inlineContent,
                maxLines = 2,
                overflow = TextOverflow.Ellipsis,
                color = Color(0xff666666), fontSize = 14.sp
            )

            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.padding(vertical = 8.dp)
            ) {
                LinearProgressIndicator(
                    progress = percent,
                    color = Color(0xff149EE7),
                    trackColor = Color(0xffeeeeee),
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(3.dp)
                        .weight(3f)
                )

                Spacer(Modifier.width(8.dp))

                Text(
                    buildAnnotatedString {
                        withStyle(SpanStyle(color = Color(0xff666666), fontSize = 10.sp)) {
                            append(desc)
                        }
                        withStyle(SpanStyle(color = Color(0xff999999), fontSize = 10.sp)) {
                            append("\t/\t")
                            append("每日上限100积分")
                        }
                    }, modifier = Modifier
                        .weight(
                            7f,
                            fill = false // 不填充剩余空间
                        )
                        .padding(horizontal = 8.dp)
                )

            }
        }
        OutlinedButton(
            onClick = {},
            border = if (percent >= 1) ButtonDefaults.outlinedButtonBorder() else BorderStroke(
                1.dp,
                color = if (percent >= 1) Color(0XFF999999) else Color(0XFFC27248)
            ),
            shape = CircleShape,
            modifier = Modifier
                .weight(2.5f),
            enabled = (percent < 1f)
        ) {
            Text(
                text = "去学习",
                color = if (percent >= 1) Color(0XFF999999) else Color(0XFFC27248)
            )
        }
    }
}

@Composable
fun DailyTaskContent() {
    // TODO: 实现每日任务内容
    // 可以使用 Column 或者 Row 等布局，也可以使用其他组件，如 Text 等
    // 这里只是一个示例，你可以根据自己的需求进行修改
    DailyTaskItem(
        title = "登录",
        secondTitle = "5积分/每日首次登录",
        desc = "已获5积分 / 每日上限5积分",
        percent = 1f
    )
    DailyTaskItem(
        title = "文章学习",
        secondTitle = "10积分/每有效阅读1篇文章",
        desc = "已获50积分/每日上限100积分",
        percent = 0.7f
    )

    DailyTaskItem(
        title = "视听学习",
        secondTitle = "10积分/每有效观看视频或收听音频累计",
        desc = "已获50积分 / 每日上限100积分",
        percent = 0.5f
    )


}

